<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 产品类别 -->
      <el-col :span="4">
        <div class="head-container">
          <el-input
            v-model="productCategoryName"
            placeholder="请输入产品类别名"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!-- 产品数据 -->
      <el-col :span="20">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <!-- <el-form-item label="产品编号" prop="productNo">
        <el-input
          v-model="queryParams.productNo"
          placeholder="请输入产品编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <el-form-item label="产品名称" prop="productName">
            <el-input
              v-model="queryParams.productName"
              placeholder="请输入产品名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="快捷码" prop="shortcutCode">
            <el-input
              v-model="queryParams.shortcutCode"
              placeholder="请输入快捷码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="产品类别" prop="productCategoryId">
        <el-input
          v-model="queryParams.productCategoryId"
          placeholder="请输入产品类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
        
      </el-form-item> -->
          <el-form-item>
            <el-button
              type="cyan"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['systembasic:product:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['systembasic:product:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['systembasic:product:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['systembasic:product:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="productList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="产品编号" align="center" prop="productNo" />
          <el-table-column label="产品名称" align="center" prop="productName" />
          <el-table-column label="快捷码" align="center" prop="shortcutCode" />
          <el-table-column
            label="产品类别"
            align="center"
            prop="productCategoryName"
          />
          <el-table-column
            label="默认客户"
            align="center"
            prop="customerName"
          />
          <el-table-column label="数量" align="center" prop="qty" />
          <el-table-column
            label="数量单位"
            align="center"
            prop="qtyUnit"
            :formatter="qtyUnitFormat"
          />
          <el-table-column label="重量" align="center" prop="weight" />
          <el-table-column
            label="重量单位"
            align="center"
            prop="weightUnit"
            :formatter="weightUnitFormat"
          />
          <el-table-column
            label="产品规格1"
            align="center"
            prop="productSpec1"
          />
          <el-table-column
            label="产品规格2"
            align="center"
            prop="productSpec2"
          />
          <el-table-column
            label="产品规格3"
            align="center"
            prop="productSpec3"
          />
          <el-table-column
            label="产品规格4"
            align="center"
            prop="productSpec4"
          />
          <el-table-column
            label="产品厚度"
            align="center"
            prop="productThick"
          />
          <el-table-column
            label="产品整箱数量"
            align="center"
            prop="qtyPerPkg"
          />
          <el-table-column
            label="是否有效"
            align="center"
            prop="deleteFlag"
            :formatter="deleteFlagFormat"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['systembasic:product:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['systembasic:product:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改产品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品编号" prop="productNo">
          <el-input
            v-model="form.productNo"
            v-bind:disabled="disableEdit"
            placeholder="请输入产品编号"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="产品名称" prop="productName">
              <el-input
                v-model="form.productName"
                placeholder="请输入产品名称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="快捷码" prop="shortcutCode">
              <el-input
                v-model="form.shortcutCode"
                placeholder="请输入快捷码"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="产品类别" prop="productCategoryId">
          <treeselect
            v-model="form.productCategoryId"
            :options="categoryOptions"
            :show-count="true"
            placeholder="请选择产品类别"
          />
        </el-form-item>
        <el-row>
          <el-col :span="18">
            <el-form-item label="默认客户" prop="customerNo">
              <el-input
                v-model="form.customerNo"
                placeholder="请选择默认客户"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6" align="center">
            <el-button type="primary" @click="handleSelectCustomer"
              >选 择</el-button
            >
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="数量" prop="qty">
              <el-input v-model="form.qty" placeholder="请输入数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量单位" prop="qtyUnit">
              <el-select v-model="form.qtyUnit" placeholder="请选择数量单位">
                <el-option
                  v-for="dict in qtyUnitOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="重量" prop="weight">
              <el-input v-model="form.weight" placeholder="请输入重量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="重量单位" prop="weightUnit">
              <el-select v-model="form.weightUnit" placeholder="请选择重量单位">
                <el-option
                  v-for="dict in weightUnitOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="产品规格1" prop="productSpec1">
              <el-input
                v-model="form.productSpec1"
                placeholder="请输入产品规格1"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品规格2" prop="productSpec2">
              <el-input
                v-model="form.productSpec2"
                placeholder="请输入产品规格2"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><el-form-item label="产品规格3" prop="productSpec3">
              <el-input
                v-model="form.productSpec3"
                placeholder="请输入产品规格3"
              /> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="产品规格4" prop="productSpec4">
              <el-input
                v-model="form.productSpec4"
                placeholder="请输入产品规格4"
              /> </el-form-item
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12"
            ><el-form-item label="产品厚度" prop="productThick">
              <el-input
                v-model="form.productThick"
                placeholder="请输入产品厚度"
              /> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="产品整箱数量" prop="qtyPerPkg">
              <el-input
                v-model="form.qtyPerPkg"
                placeholder="请输入产品整箱数量"
              /> </el-form-item
          ></el-col>
        </el-row>

        <el-form-item label="是否有效">
          <el-radio-group v-model="form.deleteFlag">
            <el-radio
              v-for="dict in deleteFlagOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
              >{{ dict.dictLabel }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 选择客户对话框 -->
    <!-- <el-dialog
      :title="selectCustomerTitle"
      :visible.sync="selectCustomerOpen"
      width="600px"
      append-to-body
    >
      <div style="height:500px">
        <el-card>
          <el-form
            :model="queryCustomerParams"
            ref="queryCustomerForm"
            :inline="true"
            label-width="68px"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="客户名" prop="customerName">
                  <el-input
                    v-model="queryCustomerParams.customerName"
                    placeholder="请输入客户名"
                    clearable
                    size="small"
                    @keyup.enter.native="handleCustomerQuery"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="快捷码" prop="shortcutCode">
                  <el-input
                    v-model="queryCustomerParams.shortcutCode"
                    placeholder="请输入快捷码"
                    clearable
                    size="small"
                    @keyup.enter.native="handleCustomerQuery"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-form-item margin-bottom="7px">
                  <el-button
                    type="cyan"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleCustomerQuery"
                    >搜索</el-button
                  >
                </el-form-item>
          </el-form>

          <el-table
            height="200px"
            ref="emtb"
            v-loading="loading"
            :data="customerList"
            @selection-change="handleCustomerSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              label="客户编号"
              align="center"
              prop="customerNo"
            />
            <el-table-column
              label="客户名称"
              align="center"
              prop="customerName"
            />
            <el-table-column
              label="快捷码"
              align="center"
              prop="shortcutCode"
            />
            <el-table-column
              label="销售人员"
              align="center"
              prop="employeeName"
              :show-overflow-tooltip="true"
            />
          </el-table>

          <pagination
            v-show="totalCustomer > 0"
            :total="totalCustomer"
            :page.sync="queryCustomerParams.pageNum"
            :limit.sync="queryCustomerParams.pageSize"
            @pagination="getCustomerList"
          />
          <el-form label-width="100px">
            <el-form-item
              style="text-align: center;margin-left:-100px;margin-top:50px;"
            >
              <el-button type="primary" @click="confirmSelect">确 定</el-button>
              <el-button @click="cancelSelectCustomer">取 消</el-button>
            </el-form-item>
          </el-form>
        </el-card>
         <el-col :span="20" :xs="24"> -->

    <!-- </el-col>
      </div>
    </el-dialog> -->
    <SelectCustomerDialog
      v-bind:open="selectCustomerOpen"
      v-on:select="confirmSelectCustomer"
      v-on:cancel="cancelSelectCustomer"
    />
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
  exportProduct
} from "@/api/systembasic/product";
import { treeselect } from "@/api/systembasic/productCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SelectCustomerDialog from "@/components/Simpsense/SelectCustomerDialog";
export default {
  name: "Product",
  components: { Treeselect, SelectCustomerDialog },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品管理表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数量单位字典
      qtyUnitOptions: [],
      // 重量单位字典
      weightUnitOptions: [],
      // 是否有效字典
      deleteFlagOptions: [],
      productCategoryName: undefined,
      // 产品类别
      categoryOptions: [],
      // 选择客户
      selectCustomerOpen: false,
      // 能否编辑编号
      disableEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productNo: null,
        productName: null,
        shortcutCode: null,
        productCategoryId: null
      },
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productNo: [
          { required: true, message: "产品编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    productCategoryName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();
    this.getDicts("qty_unit").then(response => {
      this.qtyUnitOptions = response.data;
    });
    this.getDicts("weight_unit").then(response => {
      this.weightUnitOptions = response.data;
    });
    this.getDicts("sys_yes_no").then(response => {
      this.deleteFlagOptions = response.data;
    });
  },
  methods: {
    /** 查询产品管理列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.categoryOptions = response.data;
        console.log(this.categoryOptions);
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.productCategoryId = data.id;
      this.getList();
    },
    // 数量单位字典翻译
    qtyUnitFormat(row, column) {
      return this.selectDictLabel(this.qtyUnitOptions, row.qtyUnit);
    },
    // 重量单位字典翻译
    weightUnitFormat(row, column) {
      return this.selectDictLabel(this.weightUnitOptions, row.weightUnit);
    },
    // 是否有效字典翻译
    deleteFlagFormat(row, column) {
      return this.selectDictLabel(this.deleteFlagOptions, row.deleteFlag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowId: null,
        productNo: null,
        productName: null,
        shortcutCode: null,
        productCategoryId: null,
        customerNo: null,
        qty: null,
        qtyUnit: null,
        weight: null,
        weightUnit: null,
        productSpec1: null,
        productSpec2: null,
        productSpec3: null,
        productSpec4: null,
        productThick: null,
        qtyPerPkg: null,
        deleteFlag: "Y",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productNo);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disableEdit = false;
      this.reset();
      this.open = true;
      this.title = "添加产品管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disableEdit = true;
      this.reset();
      const productNo = row.productNo || this.ids;
      getProduct(productNo).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rowId != null) {
            updateProduct(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productNos = row.productNo || this.ids;
      this.$confirm(
        '是否确认删除产品管理编号为"' + productNos + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delProduct(productNos);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有产品管理数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportProduct(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    },
    handleSelectCustomer() {
      this.selectCustomerOpen = true;
    },
    // 取消选择客户按钮
    cancelSelectCustomer() {
      this.selectCustomerOpen = false;
    },
    confirmSelectCustomer(data) {
      this.selectCustomerOpen = false;
      this.form.customerNo = data.customerNo;
    }
  }
};
</script>
